package nsu.grandfinal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class Tab1 extends Activity {
	private ListView						myListview;
	private String							Data;
	private int									count	= 10;
	private CustomListAdapter		adapter;
	private NaverParser					naverPaser;
	private DaumParser					DaumParser;
	private ArrayList<XmlData>	m_xmlData;
	private ArrayList<GeoData>	m_geoData;
	private Button							poiButton;
	private EditText						poiText;
	private List<Address>				locs;
	private LocationListAdapter	locAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchlayout);
		poiText = (EditText) findViewById(R.id.TS);
		poiButton = (Button) findViewById(R.id.SB);
		
		poiButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				CheckLocation ckl = new CheckLocation(poiText.getText().toString());
				locs = ckl.findLoc();
				if (locs.size() == 0) {
					myListview = (ListView) findViewById(R.id.SL);
					naverPaser = new NaverParser();
					DaumParser = new DaumParser();
					Data = poiText.getText().toString();
					getNewList(Data, count);
					myListview.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
							int Last = adapter.getCount();
							Log.i("NET", Last + "Parsing...");
							if (Last > 1) {
								if ((Last - 1) == arg2) {
									count = count + 10;
									getNewList(Data, count);
								} else {
									Intent mView = new Intent(Tab1.this, DaumMapActivity.class);
									Bundle myBundle = new Bundle();
									String[] StringArrayData = StringArrayData(arg2);
									getGeoData(StringArrayData[3], StringArrayData[4]);
									double[] GeoArrayData = GeoArrayData();
									Log.i("GeoArrayData[0],GeoArrayData[1]:", GeoArrayData[0] + "," + GeoArrayData[1]);
									myBundle.putStringArray("StringArrayData", StringArrayData);
									myBundle.putDoubleArray("GeoArrayData", GeoArrayData);
									mView.putExtras(myBundle);
									mView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
									startActivity(mView);
								}
							}
						}
					});
				} else {
					myListview = (ListView) findViewById(R.id.SL);
					getNewList();
					myListview.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
							String featureName = locs.get(arg2).getFeatureName();
							String address = locs.get(arg2).getAddressLine(0);
							String[] StringArrayData = new String[5];
							StringArrayData[0] = featureName;
							StringArrayData[2] = address;
							double[] GeoArrayData = new double[2];
							GeoArrayData[0] = locs.get(arg2).getLatitude();
							GeoArrayData[1] = locs.get(arg2).getLongitude();
							Intent mView = new Intent(Tab1.this, DaumMapActivity.class);
							Bundle myBundle = new Bundle();
							myBundle.putStringArray("StringArrayData", StringArrayData);
							myBundle.putDoubleArray("GeoArrayData", GeoArrayData);
							myBundle.putString("sender", "Google");
							mView.putExtras(myBundle);
							mView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							startActivity(mView);
						}
					});
				}
			}
		});
	}
	
	public void getNewList(String searchTxt, int NumberOfList) {
		m_xmlData = naverPaser.GetXmlData(searchTxt, count);
		adapter = new CustomListAdapter(this, R.layout.searchlayout2, m_xmlData);
		myListview.setAdapter(adapter);
	}
	
	public void getNewList() {
		locAdapter = new LocationListAdapter(this, R.layout.searchlayout2, locs);
		myListview.setAdapter(locAdapter);
	}
	
	public void getGeoData(String a, String b) {
		m_geoData = DaumParser.getGeoData(a, b);
	}
	
	public String[] StringArrayData(int arg2) {
		XmlData xmlData = m_xmlData.get(arg2);
		String[] StringArrayData = new String[5];
		StringArrayData[0] = xmlData.d_title;
		StringArrayData[1] = xmlData.d_phone;
		StringArrayData[2] = xmlData.d_address;
		StringArrayData[3] = xmlData.d_mapx;
		StringArrayData[4] = xmlData.d_mapy;
		return StringArrayData;
	}
	
	public double[] GeoArrayData() {
		GeoData geoData = m_geoData.get(0);
		double[] GeoArrayData = new double[2];
		GeoArrayData[1] = geoData.d_dx;
		GeoArrayData[0] = geoData.d_dy;
		return GeoArrayData;
	}
	
	class CheckLocation {
		public CheckLocation(String search) {
			this.search = search;
		}
		
		private String				search;
		private Geocoder			coder;
		private List<Address>	locs;
		
		private List<Address> findLoc() {
			try {
				locs = new ArrayList<Address>();
				coder = new Geocoder(Tab1.this, Locale.KOREA);
				locs = coder.getFromLocationName(search, 20);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return locs;
		}
	}
}